<?php
class Acrpermission extends AppModel{
	public $name = 'Acrpermission';
	var $permissions = array();
	
	public $belongsTo = array(
		'User' => array(
            'className'  => 'User',
        ),
        'Acpermission' => array(
            'className'  => 'Acpermission',
        )
	);
	
	public function loadPermissions() {
		$ps = $this->find("all");
		//debug($ps);
		
		foreach ($ps as $key => $acr) {
			$userKey = $acr['User']['username'];
			$permissions[$userKey] = empty($permissions[$userKey]) ? array(): $permissions[$userKey];
			$p4U =& $permissions[$userKey];
			$p4U[$acr['Acpermission']['alias']] = 1;
		}
		
		return $permissions;
	}
	
	public function checkPermission ($userName, $permission) {
		return empty($permissions[$userName])?false: empty($permissions[$userName][$permission]) ? false : true;
	}
	
	public function setPermission ($acrPermission) {
		$this->create();
		$this->save($acrPermission);
	}
	
	public function removePermission ($acrPermission) {
		// debug($acrPermission);
		$permission = $this->find("list",array('fields'=>array('user_id','id'), 'conditions' => array('user_id' => $acrPermission['user_id'], 'acpermission_id' => $acrPermission['acpermission_id'])));
		// debug($permission);
		$this->create();
		$this->delete($permission[$acrPermission['user_id']]);
	}
}

